package commander;

import gitobject.*;
import fileoperation.FileDeletion;
import repository.Repository;

import java.io.*;

public class init extends Command{
    public void init(){
        File f = new File(path);
        if(f.exists()) System.out.println("this directory already initialted");
//        File objects = new File(f,"objects");
        File objects = new File(Repository.getGitDir() + File.separator + "objects" );
        //path上创建objects文件夹
//        File commits = new File(Repository.getGitDir() + File.separator + "objects" + File.separator + "commits");
//        File refs = new File(f,"refs");
        File refs = new File(Repository.getGitDir() + File.separator + "refs" );
        //path上创建refs文件夹
//        File heads = new File(refs, "heads");
        File heads = new File(Repository.getGitDir() + File.separator + "refs" + File.separator + "heads");
        //refs上创建heads文件夹
        try{
            refs.mkdirs();
            objects.mkdirs();
            heads.mkdirs();
//            commits.mkdirs();
            //index.createNewFile();
        }catch (Exception e) {
            e.printStackTrace();
        }

        Branch master = new Branch("master","");
     //   Branch master = new Branch();
     //   master.setName("master");
        //新建master分支
        Head head = new Head("master");
        //新建Head头指针

        if(f.mkdir()&&objects.mkdir()&&refs.mkdir()&&heads.mkdir()){
            System.out.println("git initiated");
        }
        try{
            master.writeObject();
            head.writeObject();
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }

}
